﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonClasses;

namespace Web 
{
    public partial class Default : System.Web.UI.Page
    {

        private Regex _regex;
        private Match _matchColl;
        private DataProvider _db;
        private StatusOrder _statOrd;

        /// <summary>
        /// вывод результатов или сообщение статуса
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e) 
        {
            //http://www.rookee.ru/web/ProxyTest.aspx
            if (Request.Params["OrderId"] != null)
            {
                int id;
                Int32.TryParse(Request.Params["OrderId"], out id);
                mainPanel.Visible = false;
                resultPanel.Visible = true;
                _db = new DataProvider();
                _statOrd = _db.CheckStatus(id);
                if (_statOrd != StatusOrder.Complete)
                {
                    showMessage.Visible = true;
                    TryResaultLink.Visible = true;
                    TryResaultLink.Text = "Посмотреть результаты с ID=" + id + " &rarr;";
                    TryResaultLink.NavigateUrl = "?OrderId=" + id;
                }
                else if (_statOrd == StatusOrder.Complete)
                {
                    Order ord = _db.GetResult(id);
                    TableResault.Visible = true;
                    for (int i = 0; i < ord.Adverts.Count; i++)
                    {
                        TableRow tr = new TableRow();
                        TableCell[] tc = new TableCell[2];
                        tc[0] = new TableCell();
                        tc[1] = new TableCell();
                        tc[0].Text = ord.Adverts[i].Text;
                        tc[1].Text = ord.Adverts[i].Position.ToString();
                        tr.Cells.AddRange(tc);
                        TableResault.Rows.Add(tr);
                    }
                }
            }

        } 

        /// <summary>
        /// запись заказа в db
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CreateOrder___(object sender, EventArgs e)
        {
            Order ord = new Order();
            ord.StartDate = DateTime.Now;
            ord.Status = StatusOrder.NotProcessed;

            _regex = new Regex(@"(?:http://)?(?:www.)?([-\w]+.\w{2,4})[/\S]*");
            _matchColl = _regex.Match(saite.Text);

            if (!_matchColl.Success)
            {
                RequiredFieldValidator1.IsValid = false;
                return;
            }
            if (advertslist.Items.Count == 0)
            {
                CustomValidator1.IsValid = false;
                return;
            }
            ord.Site = _matchColl.Groups[1].Value;

            foreach (var adv in advertslist.Items)
            {
                ord.Adverts.Add(new Advert(adv.ToString()));
            }
            int id = DataProvider.Instance.AddOrder(ord);
            ResaultlLink.Visible = true;
            ResaultlLink.Text = "Посмотреть результаты с ID=" + id + " &rarr;";
            ResaultlLink.NavigateUrl = "?OrderId=" + id;
        }


        /// <summary>
        /// добавление адверта в список
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            advertslist.Items.Add(advtxt.Text);
            advtxt.Text = null;
        }

        /// <summary>
        /// удаление ненужного адверта из списка
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void advertslist_SelectedIndexChanged(object sender, EventArgs e)
        {
            advertslist.Items.Remove(advertslist.SelectedValue);
        }


    }
}